package leetcode_2501_2600;

import java.util.HashSet;
import java.util.Set;

public class LeeCode_2543 {
    public static void main(String[] args) {
        System.out.println(isReachable(6, 9));
        System.out.println(isReachable(4, 7));
    }
    private static boolean isReachable(int targetX, int targetY) {
        int i = 1;
        Set<Integer> set1 = new HashSet<>();
        while (i * i <= targetX){
            if (targetX % i == 0){
                if (charge(i))
                    set1.add(i);
                if (charge(targetX / i))
                    set1.add(targetX / i);
            }
            i++;
        }
        i = 1;
        while (i * i <= targetY){
            if (targetY % i == 0){
                if (charge(i) && set1.contains(i)){
                    return false;
                }
                if (charge(targetY / i) && set1.contains(targetY / i)) {
                    return false;
                }
            }
            i++;
        }
        return true;
    }
    private static boolean charge(int num){
        if (num == 2 || num == 1)
            return false;
        int i = 2;
        while (i * i <= num){
            if (num % i == 0){
                return false;
            }
            i++;
        }
        return true;
    }
}
